package com.ghc.androidexample.navigation;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ghc.androidexample.R;

public class SwipeActivity extends FragmentActivity {
	private ViewPager mViewPager;
	private static final int MAX_ITEMS = 5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_swipe);

		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayHomeAsUpEnabled(true);

		mViewPager = (ViewPager) findViewById(R.id.vpItems);
		ItemsPagerAdpter adpter = new ItemsPagerAdpter(
				getSupportFragmentManager());
		mViewPager.setAdapter(adpter);
		mViewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

					@Override
					public void onPageSelected(int position) {
						getActionBar().setSelectedNavigationItem(position);
					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub

					}
				});

		// Create a tab listener that is called when the user changes tabs.
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
				mViewPager.setCurrentItem(tab.getPosition());
			}

			public void onTabUnselected(ActionBar.Tab tab,
					FragmentTransaction ft) {
				// hide the given tab
			}

			public void onTabReselected(ActionBar.Tab tab,
					FragmentTransaction ft) {
				// probably ignore this event
			}
		};

		// Add 3 tabs, specifying the tab's text and TabListener
		for (int i = 0; i < MAX_ITEMS; i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(ItemFragment.ITEM_NAME + "#" + i)
					.setTabListener(tabListener));
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.swipe, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = NavUtils.getParentActivityIntent(this);
			if (NavUtils.shouldUpRecreateTask(this, intent)) {
				TaskStackBuilder.create(this)
						.addNextIntentWithParentStack(intent).startActivities();
			} else {
				NavUtils.navigateUpTo(this, intent);
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private class ItemsPagerAdpter extends FragmentStatePagerAdapter {

		/**
		 * @param fm
		 */
		public ItemsPagerAdpter(FragmentManager fm) {
			super(fm);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.support.v4.app.FragmentStatePagerAdapter#getItem(int)
		 */
		@Override
		public Fragment getItem(int position) {
			Fragment fragment = new ItemFragment();
			Bundle bundle = new Bundle();
			bundle.putString("name", ItemFragment.ITEM_NAME + "#" + position);
			fragment.setArguments(bundle);

			return fragment;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
		public int getCount() {
			return MAX_ITEMS;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return ItemFragment.ITEM_NAME + "#" + position;
		}
	}

	private class ItemFragment extends Fragment {
		public static final String ITEM_NAME = "Item";

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.swipe_item, container, false);

			Bundle bundle = getArguments();
			if (bundle != null && bundle.get("name") != null) {
				String name = bundle.getString("name");
				TextView tvName = (TextView) view.findViewById(R.id.tvName);
				tvName.setText(name);
			}

			return view;
		}
	}

}
